﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using EnvDTE;
using Microsoft.VisualStudio.Shell;

namespace Agile.VSIX {
    internal class CodeCleanupManager {
        internal static void Cleanup(TextDocument textDocument) {
            RemoveOnewayNote(textDocument);
            RemoveHtmlNote(textDocument);
            RemoveMultiLineNote(textDocument);
            FileHeaderExecute(textDocument);
            RemoveNullLine(textDocument);
        }
        // 单行注释    
        private static void RemoveOnewayNote(TextDocument textDocument) {
            const string pattern = @"[^:]//{1,2}[\s\S]*?\n";
            string replacement = Environment.NewLine;
            TextDocumentHelper.SubstituteAllStringMatches(textDocument,pattern,replacement);
        }
        // <!-- -->    
        private static void RemoveHtmlNote(TextDocument textDocument) {
            const string pattern = @"<!--(.|[\r\n])*?-->";
            string replacement = Environment.NewLine;
            TextDocumentHelper.SubstituteAllStringMatches(textDocument,pattern,replacement);
        }
        // /*  */ 多行注释
        private static void RemoveMultiLineNote(TextDocument textDocument) {
            const string pattern = @"/\*{1,2}[\s\S]*?\*/"; 
            string replacement = Environment.NewLine;
            TextDocumentHelper.SubstituteAllStringMatches(textDocument,pattern,replacement);
        }
        // \r\n\r\n
        private static void RemoveNullLine(TextDocument textDocument) {
            const string pattern = @"^\s*\n";
            string replacement = Environment.NewLine;
            TextDocumentHelper.SubstituteAllStringMatches(textDocument,pattern,replacement);
        }
        internal static void FileHeaderExecute(TextDocument textDocument) {
            ThreadHelper.ThrowIfNotOnUIThread();

            var selection=textDocument.Selection;
            var ac = selection.ActivePoint.AbsoluteCharOffset;
            selection.SelectAll();
            selection.MoveToAbsoluteOffset(ac);
           
            #region GetTemplate
            var dir = Assembly.GetCallingAssembly().CodeBase.Replace("file:///",string.Empty);
            var file = Path.Combine(Path.GetDirectoryName(dir),"Agile_VSIX_FileHeaderTemplate.xml");
            if(!File.Exists(file)) {
                File.WriteAllText(file,@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<FileHeaderTemplate>
    <NowFormat>yyyy-MM-dd</NowFormat>
    <Content>
        <![CDATA[/*******************************************************
 * 作    者：罗敏贵
 * 运行环境：$Environment$
 * 操作系统：$OS$
 * 版 本号：$verion$
 * 文件说明：$end$
 * 历史记录：
 *     创建文件 罗敏贵 $Now$
 * 
*******************************************************/
]]>
    </Content>
</FileHeaderTemplate>");
            }
            var xml = File.ReadAllText(file);
            var doc = XDocument.Load(new StringReader(xml));
            var nowFormat = doc.Descendants("NowFormat").First().Value;
            var content = doc.Descendants("Content").First().Value;

            content = content.Replace("$Environment$",RuntimeInformation.FrameworkDescription);
            content = content.Replace("$OS$",Environment.OSVersion.VersionString);
            content = content.Replace("$verion$",Environment.Version.ToString());
            content = content.Replace("$Now$",DateTime.Now.ToString(nowFormat));
            content = content.Replace("$end$","");
            #endregion
          
            selection.StartOfDocument();
            selection.Insert(content);

            selection.SmartFormat();
        }
    }
}
